<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="js/vue.js"></script>
    <script src="js/axios-0.18.0.js"></script>
</head>
<body>
    <div id="app">
        username: <input type="text" name="username" v-model="user.username"> <br>
        password: <input type="text" name="password" v-model="user.password"> <br>

        <input type="button" value="登录" @click="login()">
    </div>
</body>
<script>
    new Vue({
        el:"#app",
        data:{
            user:{
                username:"",
                password:""
            }
        },
        methods:{
            login(){
                //发送请求时，允许携带cookie信息（后台是基于session保存登录用户）
                axios.defaults.withCredentials = true;

                axios({
                    url:"http://localhost:81/login",
                    method:"post",
                    //向后台携带参数的方式是key=value&key2=value2...
                    params:this.user
                }).then(resp=>{
                    let r = resp.data;
                    if (r.code == 3001){
                        //用户名错误
                        alert(r.msg)
                    }else if (r.code == 3002){
                        //密码错误
                        alert(r.msg)
                    }else{
                        //登录成功
                        location.href = "index.html";
                    }
                })
            }
        }
    });
</script>
</html>